package com.woniuxy.controller;

import com.sun.deploy.security.ValidationState;
import com.woniuxy.entity.dto.DictionaryDTO;
import com.woniuxy.entity.dto.operations.VehicleDisplayDTO;
import com.woniuxy.service.VehicleService;
import com.woniuxy.util.CodeUtil;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @BelongsProject: charging-management-platform
 * @BelongsPackage: com.woniuxy.controller
 * @Author: Quan
 * @Date: 2025/4/1 10:43
 */
@RestController
@RequestMapping("/vehicle")
@AllArgsConstructor
public class VehicleController {
	private VehicleService vehicleService;
	private CodeUtil codeUtil;

	/**
	 * @Description: 查询所有车辆用于展示
	 * @Param: [vehicleDisplayDTO]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/1
	 */
	@PostMapping("/queryAll")
	public ResponseData<?> queryAll(@RequestBody PageParam<VehicleDisplayDTO> pageParam) {
		return vehicleService.queryAll(pageParam);
	}

	@GetMapping("/getCode")
	public ResponseData<?> getCode() {
		return ResponseData.ok(codeUtil.generateRegularCode("VEHICLE"));
	}

	@PostMapping("/upload")
	public ResponseData<?> uploadFile(MultipartFile file) {
		try {
			return vehicleService.uploadImg(file);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * @Description: 添加车辆信息到数据库，同时将前端传的图片地址储存到vehicle_img中
	 * @Param: [vehicleDisplayDTO]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/1
	 */
	@PostMapping("/add")
	public ResponseData<?> add(@RequestBody VehicleDisplayDTO vehicleDisplayDTO) {
		return vehicleService.addVehicle(vehicleDisplayDTO);
	}

	/**
	 * @Description: 通过id,把图片与车辆信息一同传到前端
	 * @Param: [id]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/1
	 */
	@GetMapping("/getById/{id}")
	public ResponseData<?> getById(@PathVariable("id") Long id) {
		return vehicleService.queryById(id);
	}

	/**
	 * @Description: 将前端的数据更新到数据库中
	 * @Param: [vehicleDisplayDTO]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/1
	 */
	@PostMapping("/update")
	public ResponseData<?> update(@RequestBody VehicleDisplayDTO vehicleDisplayDTO) {
		return vehicleService.update(vehicleDisplayDTO);
	}

	/**
	 * @Description: 当前端点击禁用启用时，对数据库中的内容进行修改
	 * @Param: [vehicleDisplayDTO]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/2
	 */
	@PostMapping("/updateVehicleCondition")
	public ResponseData<?> updateVehicleCondition(@RequestBody VehicleDisplayDTO vehicleDisplayDTO) {
		return vehicleService.updateVehicleCondition(vehicleDisplayDTO);
	}

	/***
	 * @Description: 通过id对车辆进行逻辑删
	 * @Param: [id]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/2
	 */
	@GetMapping("/delete/{id}")
	public ResponseData<?> delete(@PathVariable("id") Long id) {
		return vehicleService.deleteById(id);
	}

	/**
	 * @Description: 通过vehicle 批量删除vehicle和img
	 * @Param: [vehicleDisplayDTOList]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/2
	 */
	@PostMapping("/batchDelete")
	public ResponseData<?> batchDelete(@RequestBody List<VehicleDisplayDTO> vehicleDisplayDTOList) {
		return vehicleService.batchDelete(vehicleDisplayDTOList);
	}
}
